package cn.vc.basicmodule.util;

import cn.vc.basicmodule.properties.LogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

@Service
public class Logging {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private LogProperties logProperties;

    /**
     * <h2>正常</h2>
     */
    public void info(String msg) {
        StringBuilder sb = new StringBuilder("\033[1m\033[32mINFO\033[0m ");
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        StackTraceElement stack = stacks[2];
        // 是否需要显示时间
        if (!logProperties.getIsShowTime()) {
            sb.append(sdf.format(new Date())).append(" ");
        }
        // 是否需要显示包地址
        if (!logProperties.getIsShowClassName()) {
            sb.append("\033[1m\033[32m[\033[0m").append(stack.getClassName()).append("\033[1m\033[32m]\033[0m ");
        }
        // 是否需要显示行数
        if (!logProperties.getIsShowLineNumber()) {
            sb.append("\033[1m\033[32m").append(stack.getLineNumber()).append("\033[0m" );
        }
        sb.append(msg);
        System.out.println(sb);
    }
    /**
     * <h2>警告</h2>
     */
    public void warn(String msg) {
        StringBuilder sb = new StringBuilder("\033[1m\033[33mINFO\033[0m ");
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        StackTraceElement stack = stacks[2];
        // 是否需要显示时间
        if (!logProperties.getIsShowTime()) {
            sb.append(sdf.format(new Date())).append(" ");
        }
        // 是否需要显示包地址
        if (!logProperties.getIsShowClassName()) {
            sb.append("\033[1m\033[33m[\033[0m").append(stack.getClassName()).append("\033[1m\033[33m]\033[0m ");
        }
        // 是否需要显示行数
        if (!logProperties.getIsShowLineNumber()) {
            sb.append("\033[1m\033[33m").append(stack.getLineNumber()).append("\033[0m" );
        }
        sb.append(msg);
        System.out.println(sb);
    }
    /**
     * <h2>错误</h2>
     */
    public void error(String msg) {
        StringBuilder sb = new StringBuilder("\033[1m\033[31mINFO\033[0m ");
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        StackTraceElement stack = stacks[2];
        // 是否需要显示时间
        if (!logProperties.getIsShowTime()) {
            sb.append(sdf.format(new Date())).append(" ");
        }
        // 是否需要显示包地址
        if (!logProperties.getIsShowClassName()) {
            sb.append("\033[1m\033[31m[\033[0m").append(stack.getClassName()).append("\033[1m\033[31m]\033[0m ");
        }
        // 是否需要显示行数
        if (!logProperties.getIsShowLineNumber()) {
            sb.append("\033[1m\033[31m").append(stack.getLineNumber()).append("\033[0m" );
        }
        sb.append(msg);
        System.out.println(sb);
    }
}
